app.controller("confirm-order", function ($scope, $http) {
    var orderId = sessionStorage.getItem('orderId');
    var token = localStorage.getItem('token');
    $scope.orderInfo = {};


    $scope.showOrderDetails = function () {
        if (!token) {
            $.toast("请先登录！");
            location.href = '#user-login';

            return;
        }

        if (token) {


            if (!orderId) {
                $.toast("找不到订单号");
                location.href = '#myorders1';
                return;
            }
            if (orderId) {
                $http({
                    method: 'get',
                    url: app.netPath + '/order/show/orderinfo',
                    headers: {Authorization: token},
                    params: {orderId: orderId}
                }).then(function (res) {
                    console.log(res.data.data);
                    if (!res.data.error) {
                        $scope.orderInfo = res.data.data;
                        $scope.orderInfo.orderWord = $scope.orderInfo.orderStatus == 0 ? '已购买' : '未购买';

                    }
                    else {
                        $.toast(res.data.msg)
                    }

                }, function () {
                    $.toast('网络走丢了！')
                })
            }

        }

    };

//购买课程
    $scope.buyClass = function () {
        $.confirm("是否购买此课程？", "", function () {

            if (!token) {
                $.alert("请先登录", function () {
                    location.href = '#user-login'
                });
                return;
            }

            if (token) {


                var orderId = sessionStorage.getItem('orderId');
                $http({
                    method: 'get',
                    url: app.netPath + '/order/payorder',
                    headers: {Authorization: token},
                    params: {orderId: orderId}
                }).then(function (res) {
                    if (!res.data.error) {
                        location.href = '#confirm-order';
                        $.toast("支付成功");
                    }
                    else {
                        $.toast(res.data.msg)

                    }

                })

            }
        }, function () {
            $.alert("交易取消")
        });


    };

    //得到订单id并删除
    $scope.getorderId = function (orderId) {

        $.confirm("是否删除此订单？", "", function () {

            if (!token) {
                $.alert("请先登录", function () {
                    location.href = '#user-login'
                });
                return;
            }

            if (token) {


                var orderId = sessionStorage.getItem('orderId');
                $http({
                    method: 'get',
                    url: app.netPath + '/order/delete',
                    headers: {Authorization: token},
                    params: {orderId: orderId}
                }).then(function (res) {
                    if (!res.data.error) {
                        location.href = '#myorders1';
                        $.toast("删除成功！");
                    }
                    else {
                        $.toast(res.data.msg)

                    }

                })

            }
        }, function () {

        });
    }

});